package com.atguigu.service_base.handler;

/**
 * 统一异常处理类
 */

import com.atguigu.common_utils.Response;
import com.atguigu.service_base.exception.GuliException;
import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class) // 捕获什么异常
    @ResponseBody                      // 返回响应体
    public Response error(Exception e){
        e.printStackTrace();
        return Response.err().message("触发类全局异常");
    }

    @ExceptionHandler(GuliException.class) // 捕获什么异常
    @ResponseBody                      // 返回响应体
    public Response guliError(GuliException e){
        e.printStackTrace();
        return Response.err().code(e.getCode()).message(e.getMessage());
    }
}